package dialog.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

public class DialogActivity extends Activity {
    private Button button1, button2, button3, button4;  
    private Button.OnClickListener listener1, listener2, listener3, listener4;  
    private final int DIALOG1 = 1, DIALOG2 = 2, DIALOG3 = 3, DIALOG4 = 4;  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        prepareListener();  
    }  
      
    protected Dialog onCreateDialog(int id) {  
        switch (id) {  
        case DIALOG1:  
            return buildDialog(this, DIALOG1);  
        case DIALOG2:  
            return buildDialog(this, DIALOG2);  
        case DIALOG3:  
            return buildDialog(this, DIALOG3);  
        case DIALOG4:  
            return buildDialog(this, DIALOG4);  
        }  
        return null;  
    }  
      
    protected void onPrepareDialog(int id, Dialog dialog) {  
        super.onPrepareDialog(id, dialog);  
    }  
      
    private Dialog buildDialog(Context context, int seq) {  
        AlertDialog.Builder builder = new AlertDialog.Builder(context);  
        builder.setIcon(R.drawable.ic_launcher);  
        if (DIALOG1 == seq) {  
            builder.setTitle("Alert Dialog with tow buttons");  
            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {  
                    setTitle("Ok");  
                }  
            });  
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {  
                    setTitle("Cancel");  
                }  
            });  
        }  
        if (DIALOG2 == seq) {  
            builder.setTitle("Dialog 2");  
            builder.setMessage("Hello Dialog 2");  
              
            // AlertDialog最多只能有3个Button  
            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {  
                    setTitle("Ok");  
                }  
            });  
            builder.setNeutralButton("Username and Password", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {  
                    setTitle("Username and Password");  
                }  
            });  
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {  
                    setTitle("Cancel");  
                }  
            });  
        }  
        if (DIALOG3 == seq) {  
            // LayoutInflater的inflate方法可以将一个XML布局变成一个View实例  
            LayoutInflater inflater = LayoutInflater.from(this);  
            View textEntryView = inflater.inflate(R.layout.alert_dialog_text_entry, null);  
            builder.setTitle("Text Entey");  
              
            // setView()真可以说是Dialog的一个精髓  
            builder.setView(textEntryView);  
            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {  
                    setTitle("Ok");  
                }  
            });  
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {  
                    setTitle("Cancel");  
                }  
            });  
        }  
        if (DIALOG4 == seq) {  
            ProgressDialog dialog = new ProgressDialog(context);  
            dialog.setTitle("Downding the songs...");  
            dialog.setMessage("Please Waiting...");  
            return dialog;  
        }  
        return builder.create();  
    }  
  
    private boolean prepareListener() {  
        // init button  
        button1 = (Button) findViewById(R.id.button1);  
        button2 = (Button) findViewById(R.id.button2);  
        button3 = (Button) findViewById(R.id.button3);  
        button4 = (Button) findViewById(R.id.button4);  
          
        // init listener  
        listener1 = new Button.OnClickListener() {  
            public void onClick(View v) {  
                showDialog(DIALOG1);  
            }  
        };  
        listener2 = new Button.OnClickListener() {  
            public void onClick(View v) {  
                showDialog(DIALOG2);  
            }  
        };  
        listener3 = new Button.OnClickListener() {  
            public void onClick(View v) {  
                showDialog(DIALOG3);  
            }  
        };  
        listener4 = new Button.OnClickListener() {  
            public void onClick(View v) {  
                showDialog(DIALOG4);  
            }  
        };  
          
        // bind listener  
        button1.setOnClickListener(listener1);  
        button2.setOnClickListener(listener2);  
        button3.setOnClickListener(listener3);  
        button4.setOnClickListener(listener4);  
        return true;  
    }  
}  
